/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.unohacha.qqj.mylibrary.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.unohacha.qqj.mylibrary.R;
import com.unohacha.qqj.mylibrary.listener.PoponDismissListener;
import com.unohacha.qqj.mylibrary.util.CommonUtils;

/**
 * 说明: 弹出确认框
 * 作者: fangkaijin on 2017/5/28.17:50
 * 邮箱: fangkaijin@gmail.com
 */

public class CommonPopWindow extends PopupWindow {

    private OnConfirmListener confirmListener;
    private Context mContext;
    private Activity aty;
    private Resources resources;

    public CommonPopWindow(Context mContext, Activity aty)
    {
        this.mContext = mContext;
        this.aty = aty;
        resources = mContext.getResources();
    }

    public void showUi(String msg, String leftBtnTag, String rightBtnTag)
    {
        View view = LayoutInflater.from ( mContext ).inflate (
                R.layout.widget_confirm,
                null
        );

        TextView messagetext = (TextView) view.findViewById(R.id.messagetext);
        RelativeLayout sureBtnL = (RelativeLayout) view.findViewById(R.id.sureBtnL);
        RelativeLayout cancelBtnL = (RelativeLayout) view.findViewById(R.id.cancelBtnL);
        TextView cancelTag = (TextView) view.findViewById(R.id.cancelTag);
        TextView sureTag = (TextView) view.findViewById(R.id.sureTag);
        cancelTag.setText((TextUtils.isEmpty(leftBtnTag)?resources.getString(R.string.cancel):leftBtnTag));
        sureTag.setText((TextUtils.isEmpty(rightBtnTag)?resources.getString(R.string.sure):rightBtnTag));
        messagetext.setText(msg);
        sureBtnL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null==confirmListener)
                {
                    dismissView();
                }
                else
                {
                    confirmListener.onSure();
                    dismissView();
                }
            }
        });

        cancelBtnL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null==confirmListener)
                {
                    dismissView();
                }
                else
                {
                    confirmListener.onCancel();
                    dismissView();
                }
            }
        });

        // 设置SelectPicPopupWindow的View
        this.setContentView ( view );
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth (WindowManager.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight (WindowManager.LayoutParams.MATCH_PARENT);
        //设置动画
        this.setAnimationStyle(R.style.animationPop01);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        CommonUtils.backgroundAlpha(aty, 0.4f);
    }

    public interface  OnConfirmListener{
        void onSure();
        void onCancel();
    }

    public void setConfirmListener(OnConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public void dismissView()
    {
        setOnDismissListener ( new PoponDismissListener( aty ) );
        dismiss ();
    }
}
